package shopping.service.impl;

import shopping.dao.ClothesDao;
import shopping.dao.impl.ClothesDaoImp;
import shopping.bean.Clothes;
import shopping.bean.Page;
import shopping.service.ClothesService;

import java.util.List;

public class ClothesServiceImpl implements ClothesService {
    private ClothesDao clothesDao = new ClothesDaoImp();
    @Override
    public void addClothes(Clothes clothes) {
        clothesDao.addClothes(clothes);
    }

    @Override
    public void deleteClothesById(Integer id) {
        clothesDao.deleteClothesById(id);
    }

    @Override
    public void updateClothes(Clothes clothes) {
        clothesDao.updateClothes(clothes);
    }

    @Override
    public Clothes queryClothesById(Integer id) {

        return clothesDao.queryClothesById(id);
    }

    @Override
    public List<Clothes> queryClothess() {
        return clothesDao.queryClothess();
    }

    @Override
    public Page<Clothes> page(int pageNo, int pageSize) {
        Page<Clothes> page = new Page<>();
        //求总记录数
        Integer pageTotalCount = clothesDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize != 0){
            pageTotal += 1;
        }
        //设置总页码数
        page.setPageTotal(pageTotal);
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示数据
        page.setPageSize(pageSize);

        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        //求当前页数据
        List<Clothes> items = clothesDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Clothes> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Clothes> page = new Page<>();
        //求总记录数
        Integer pageTotalCount = clothesDao.queryForPageTotalCountByPrice(min,max);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码数
        Integer pageTotal = pageTotalCount / pageSize;
        if(pageTotalCount % pageSize != 0){
            pageTotal += 1;
        }
        //设置总页码数
        page.setPageTotal(pageTotal);
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示数据
        page.setPageSize(pageSize);

        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        //求当前页数据
        List<Clothes> items = clothesDao.queryForPageItemsByPrice(begin,pageSize,min,max);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Clothes> pageByName(int pageNo, int pageSize, String name) {
        Page<Clothes> page = new Page<>();
        //求总记录数
        Integer pageTotalCount = clothesDao.queryForPageTotalCountByName(name);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        if (pageTotalCount == 0){
            Page<Clothes> page1 = page(1, 4);
            List<Clothes> items = page1.getItems();
            page.setItems(items);
        }else {
            //求总页码数
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize != 0){
                pageTotal += 1;
            }
            //设置总页码数
            page.setPageTotal(pageTotal);
            //设置当前页码
            page.setPageNo(pageNo);
            //设置每页显示数据
            page.setPageSize(pageSize);

            // 求当前页数据的开始索引
            int begin = (page.getPageNo() - 1) * pageSize;
            //求当前页数据
            List<Clothes> items = clothesDao.queryForPageItemsByName(begin,pageSize,name);
            page.setItems(items);
        }

        return page;
    }

    @Override
    public Page<Clothes> pageByKind(int pageNo, int pageSize, String kind) {
        Page<Clothes> page = new Page<>();
        //求总记录数
        Integer pageTotalCount = clothesDao.queryForPageTotalCountByKind(kind);
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        if (pageTotalCount == 0){
            Page<Clothes> page1 = page(1, 4);
            List<Clothes> items = page1.getItems();
            page.setItems(items);
        }else {
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize != 0){
                pageTotal += 1;
            }
            //设置总页码数
            page.setPageTotal(pageTotal);
            //设置当前页码
            page.setPageNo(pageNo);
            //设置每页显示数据
            page.setPageSize(pageSize);

            // 求当前页数据的开始索引
            int begin = (page.getPageNo() - 1) * pageSize;
            //求当前页数据
            List<Clothes> items = clothesDao.queryForPageItemsByKind(begin,pageSize,kind);
            page.setItems(items);
        }
        //求总页码数

        return page;
    }

    @Override
    public Clothes queryClothesByName(String name) {
        Clothes clothes = clothesDao.queryClothesByName(name);
        return clothes;
    }
}
